<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="BusinessName">
        <el-input
          v-model="queryParams.BusinessName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.bm_member_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="营业状态" prop="workState">
        <el-select v-model="queryParams.workState" placeholder="请选择营业状态" clearable>
          <el-option
            v-for="dict in dict.type.bm_member_workState"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电话号码" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="老板姓名" prop="trueName">
        <el-input
          v-model="queryParams.trueName"
          placeholder="请输入老板姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标记 " prop="isDeleted">
        <el-select v-model="queryParams.isDeleted" placeholder="请选择删除标记 " clearable>
          <el-option
            v-for="dict in dict.type.bm_member_isDeleted"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:member:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:member:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:member:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="#" align="center" prop="id"/>
      <el-table-column label="店铺名称" align="center" prop="businessName"/>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="老板姓名" align="center" prop="trueName"/>
      <el-table-column label="电话号码" align="center" prop="tel"/>
      <el-table-column label="所在城市" align="center" prop="address"/>
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bm_member_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="营业状态" align="center" prop="workState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bm_member_workState" :value="scope.row.workState"/>
        </template>
      </el-table-column>
      <el-table-column label="删除标记 " align="center" prop="isDeleted">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bm_member_isDeleted" :value="scope.row.isDeleted"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:member:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="BusinessName">
          <el-input v-model="form.BusinessName" placeholder="请输入店铺名称"/>
        </el-form-item>
        <el-form-item label="配送金额" prop="deliveryFee">
          <el-input v-model="form.deliveryFee" placeholder="请输入配送金额"/>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入店铺地址"/>
        </el-form-item>
        <el-form-item label="店铺图片" prop="backImg" required>
          <el-upload ref="backImg" :file-list="backImgfileList"
                     :action="backImgAction" :auto-upload="false"
                     :before-upload="backImgBeforeUpload"
                     list-type="picture-card"
                     accept="image/*">
            <i class="el-icon-plus"></i>
            <div slot="tip" class="el-upload__tip">只能上传不超过 10MB 的image/*文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="地址坐标" prop="address">
          <el-autocomplete
            v-model="mapLocation.address"
            :fetch-suggestions="querySearch"
            placeholder="请输入详细地址"
            style="width: 100%"
            :trigger-on-focus="false"
            @select="handleSelect2"
          />
          <div style="margin: 5px">
            <baidu-map
              class="bm-view"
              :center="mapCenter"
              :zoom="mapZoom"
              :scroll-wheel-zoom="true"
              @ready="handlerBMap">
              <bm-geolocation @locationSuccess="locationSuccess" anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
                              :showAddressBar="true" :autoLocation="true"></bm-geolocation>
            </baidu-map>
          </div>
        </el-form-item>
        <el-form-item label="地址坐标" prop="mapCode">
          <el-input v-model="form.mapCode" placeholder="请输入地址坐标"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.bm_member_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业状态" prop="workState">
          <el-select v-model="form.workState" placeholder="请选择营业状态">
            <el-option
              v-for="dict in dict.type.bm_member_workState"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话号码"/>
        </el-form-item>
        <el-form-item label="登陆密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入登陆密码"/>
        </el-form-item>
        <el-form-item label="老板姓名" prop="trueName">
          <el-input v-model="form.trueName" placeholder="请输入老板姓名"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
                          v-model="form.addTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记 " prop="isDeleted">
          <el-select v-model="form.isDeleted" placeholder="请选择删除标记 ">
            <el-option
              v-for="dict in dict.type.bm_member_isDeleted"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BaiduMap from 'vue-baidu-map/components/map/Map.vue';
import {listMember, delMember, addMember, updateMember} from "@/api/manage/member";

export default {
  name: "Member",
  dicts: ['bm_member_isDeleted', 'bm_member_state', 'bm_member_workState'],
  data() {
    return {
      mapZoom: 15,
      mapCenter: {lng: 0, lat: 0},
      mapLocation: {
        address: undefined,
        coordinate: undefined
      },
      backImgAction: 'https://jsonplaceholder.typicode.com/posts/',
      backImgfileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      id: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        BusinessName: null,
        state: null,
        workState: null,
        tel: null,
        trueName: null,
        isDeleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        console.log(response);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        BusinessName: null,
        deliveryFee: null,
        deliveryRadius: null,
        address: null,
        backImg: null,
        mapCode: null,
        state: null,
        workState: null,
        tel: null,
        password: null,
        trueName: null,
        shopType: null,
        token: null,
        amount: null,
        addTime: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.id = row.id;
      console.log("id1" + row.id);
      console.log("id2" + this.id);
      listMember(this.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商家信息编号为"' + ids + '"的数据项？').then(function () {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //图片上传
    backImgBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 10
      if (!isRightSize) {
        this.$message.error('文件大小超过 10MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    handlerBMap({BMap, map}) {
      this.BMap = BMap
      this.map = map
      if (this.mapLocation.coordinate && this.mapLocation.coordinate.lng) {
        this.mapCenter.lng = this.mapLocation.coordinate.lng
        this.mapCenter.lat = this.mapLocation.coordinate.lat
        this.mapZoom = 15
        map.addOverlay(new this.BMap.Marker(this.mapLocation.coordinate))
      } else {
        this.mapCenter.lng = 113.271429
        this.mapCenter.lat = 23.135336
        this.mapZoom = 10
      }
    },
    locationSuccess({point, AddressComponent, marker}) {
      console.log("定位成功", point, AddressComponent, marker)
      this.form.mapCode = point.lng + "," + point.lat;
    },
    handleSelect2(item) {
      var {point} = item
      this.mapLocation.coordinate = point
      this.makerCenter(point)
    },
    querySearch(queryString, cb) {
      var that = this
      var myGeo = new this.BMap.Geocoder()
      myGeo.getPoint(queryString, function (point) {
        if (point) {
          that.mapLocation.coordinate = point
          that.makerCenter(point)
        } else {
          that.mapLocation.coordinate = null
        }
      }, this.locationCity)
      var options = {
        onSearchComplete: function (results) {
          if (local.getStatus() === 0) {
            // 判断状态是否正确
            var s = []
            for (var i = 0; i < results.getCurrentNumPois(); i++) {
              var x = results.getPoi(i)
              var item = {value: x.address + x.title, point: x.point}
              s.push(item)
              cb(s)
            }
          } else {
            cb()
          }
        }
      }
      var local = new this.BMap.LocalSearch(this.map, options)
      local.search(queryString)
    },
  }
};
</script>
<style>
.bm-map {
  width: 100%;
  height: 300px;
}
</style>
